<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="100px"
    >
      <el-form-item label="耗材编号" prop="code">
        <el-select
          v-model="queryParams.code"
          class="!w-240px"
          placeholder="请选择"
          ref="select"
          popper-class="select-popper"
        >
          <el-option
            v-for="item in infoList"
            :key="item.id"
            :value="item.code"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="stuId">
        <el-input
          v-model.number="queryParams.stuId"
          class="!w-240px"
          clearable
          placeholder="请输入负责人"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh"/>
          重置
        </el-button>
        <el-button plain type="primary" @click="openForm('create')">
          <Icon class="mr-5px" icon="ep:plus"/>
          新增
        </el-button>

        <el-button :loading="exportLoading" plain type="success" @click="handleExport">
          <Icon class="mr-5px" icon="ep:download"/>
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="耗材编号" prop="code"/>
      <el-table-column align="center" label="入库数量" prop="inNum"/>
      <el-table-column align="center" label="入库时间" prop="inTime"/>
      <el-table-column align="center" label="负责人" prop="labUser"/>
      <el-table-column align="center" label="备注" prop="remark"/>
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update',scope.row.id)">修改</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RoleForm ref="formRef" @success="getList"/>
  <!-- 表单弹窗：文件上传 -->
  <FileForm ref="formUpdateRef" @success="getList"/>
</template>
<script lang="ts" setup>
import download from '@/utils/download'
import * as RoleApi from '@/api/system/role'
import * as ProjectApi from '@/api/laboratory/project'
import * as AddApi from '@/api/consumable/add'
import RoleForm from './RoleForm.vue'
import FileForm from './UploadForm.vue'
import * as InfoApi from "@/api/consumable/info";

defineOptions({name: 'SystemRole'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const infoTotal = ref(0)
const infoList = ref([])

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
})
const infoParams = reactive({
  pageNo: 1,
  pageSize: 10,
})
let dropdownWrap = null;
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询角色列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AddApi.getAddPage(queryParams)
    list.value = data.result
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const getInfoList = async () => {
  try {
    const data = await InfoApi.getInfoPage(infoParams)
    infoList.value = data.result
    infoTotal.value = data.total
  } finally {
  }
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 上传文件操作 */
const formUpdateRef = ref()
const openFormUpdate = () => {
  formUpdateRef.value.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AddApi.deleteAdd({id: id})
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}

// /** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await RoleApi.exportRole(queryParams)
    download.excel(data, '角色列表.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const handleDownload = async () => {
  // download.word(data, '角色列表.word')
}

const handleScroll = () => {
    if (dropdownWrap) {
      const {scrollTop, scrollHeight, clientHeight} = dropdownWrap;
      if (scrollTop + clientHeight >= scrollHeight) {
        // 滚动条到达底部的处理逻辑，例如加载更多数据
        infoParams.pageSize += 10
        getInfoList()
      }
    }
  }
;
const updateDropdownWrap = () => {
  dropdownWrap = document.querySelector('.select-popper .el-select-dropdown__wrap');
  if (dropdownWrap) {
    dropdownWrap.addEventListener('scroll', handleScroll);
  }
};

/** 初始化 **/
onMounted(() => {
  getList()
  getInfoList()
  nextTick(() => {
    updateDropdownWrap();
  });
})
onBeforeUnmount(() => {
  if (dropdownWrap) {
    dropdownWrap.removeEventListener('scroll', handleScroll);
  }
});
</script>
